//
//  ShareTableViewCell.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/3.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "ShareTableViewCell.h"
#import "DeviceUser.h"

@interface ShareTableViewCell ()

@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UIButton *button;

@end

@implementation ShareTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self masLayoutSubviews];
    }
    return self;
}



- (void)masLayoutSubviews{
    __weak UIView *superView = self.contentView;
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.right.equalTo(self).with.offset(-px1080Width(46));
        make.height.mas_equalTo(px1920Height(80));
        make.width.mas_equalTo(px1080Width(250));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(46));
        make.height.mas_equalTo(px1920Height(45));
        make.right.equalTo(self.button.mas_left).with.offset(-px1080Width(20));
    }];
}

- (void)setDeviceUser:(DeviceUser *)deviceUser {
    _deviceUser = deviceUser;
    self.nameLabel.text = deviceUser.username;
    
    if ([deviceUser.uid isEqualToString:self.adminUid]) {
        [_button setImage:[UIImage imageNamed:@"icon_manager"] forState:UIControlStateNormal];
        [_button setTitle:@"管理员" forState:UIControlStateNormal];
    } else {
        [_button setTitle:@"" forState:UIControlStateNormal];
        [_button setImage:[UIImage imageNamed:@"button_delete"] forState:UIControlStateNormal];
    }
}

- (void) didClickDeleteButton{
    if (![self.adminUid isEqualToString:self.deviceUser.uid]) {
        if (self.deleteBlock) {
            self.deleteBlock();
        }
    }

}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"ShareTableViewCell";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (UILabel *)nameLabel
{
    if(!_nameLabel)
    {
        UIView *superView = self.contentView;
        _nameLabel = [[UILabel alloc]init];
        [superView addSubview:_nameLabel];
        [_nameLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        //        [_nameLabel setTextColor:<#(UIColor * _Nullable)#>];
        _nameLabel.text = @"13715881560";
    }
    return _nameLabel;
}

- (UIButton *)button
{
    if(!_button)
    {
        UIView *superView = self.contentView;
        _button = [[UIButton alloc]init];
        [superView addSubview:_button];
        [_button setImage:[UIImage imageNamed:@"icon_manager"] forState:UIControlStateNormal];
        [_button setTitle:@"管理员" forState:UIControlStateNormal];
//        [_button setImage:[UIImage imageNamed:@"button_delete"] forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_button.titleLabel setFont:[UIFont systemFontOfSize:px1080Width(42)]];
        [_button addTarget:self action:@selector(didClickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
